package com.jin.gulimalltestssoclient.controller;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpSession;
import java.util.ArrayList;
import java.util.List;

@Controller
public class HelloController{

    @Value("${sso.ssoserver.url}")
    String ssoserverUrl;

    @ResponseBody
    @GetMapping("/hello")
    public String hello(){
        return "hello";
    }

    @GetMapping("/employees")
    public String employees(Model model, HttpSession session){
        Object loginUser = session.getAttribute("loginUser");
        if(StringUtils.isEmpty(loginUser)) {
            //未登录,跳转到中央服务器进行登录，然后再跳转回来
            return "redirect:"+ssoserverUrl+"?redirect_url=http://client1.com:8081/employees";
        }else{
            //已登录
            List<String> emps = new ArrayList<>();
            emps.add("丁程鑫");
            emps.add("宋亚轩");
            emps.add("刘耀文");
            model.addAttribute("emps", emps);
            return "list";
        }
    }

}
